package top.chenfu1201.mapper.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.chenfu1201.mapper.entity.UserEntity;
import top.chenfu1201.mapper.service.UserServiceImpl;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @GetMapping("/queryAll")
    public Object queryAll() {
        log.info("queryAll......");
        return this.userService.queryUsers();
    }

    @GetMapping("find/{userId}")
    public Object findUserById(@PathVariable Long userId) {
        log.info("findUserById入参userId=" + userId);
        return this.userService.findUserById(userId);
    }

    @PostMapping("/add")
    public Object addUser(@RequestBody UserEntity userEntity) {
        UserEntity user = (UserEntity) this.userService.addUser(userEntity);
        if (Objects.isNull(user)) {
            System.out.println("插入失败");
            Map<String, String> map = new HashMap<>();
            map.put("msg", "插入失败");
            return map;
        }
        return user;
    }

}
